<?php

class Adept_Component_ListBox extends Adept_Component_ValueControl
{

    protected $itemTitles = array();
    protected $itemValues = array();
    
    protected $selected = null;
    protected $size = null;
    
    protected $multiple = null;
    
    public function addItem($value, $title) 
    {
        $index = count($this->itemTitles) - 1;
    	$this->itemTitles[$index] = $title;
    	$this->itemValues[$index] = $value;
    }
    
    public function addItems($items)
    {
    	foreach ($items as $value => $title) {
    	    $this->addItem($value, $title);    
    	}
    }

    public function getItemsArray() 
    {
        $itemsArray = new Adept_Component_SelectItemsArray($this);
    	return $itemsArray;
    }	
    
    public function isItemSelected($value) 
    {
        if ($this->isMultiple()) {
            return in_array($value, $this->getSelected());
        } else {
            return $this->getSelected() == $value;
        }
    }	
    
    public function getSelected() 
    {
        if (($binding = $this->getBinding()) != null) {
            return $binding->getValue();
        } else {
    	   return $this->getValue();
        }
    }	
    
    public function isMultiple() 
    {
        if (!is_null($this->multiple)) {
            return $this->multiple;
        }
        return $this->getValueOfBinding('multiple', false);
    }
    
    public function setMultiple($multiple) 
    {
        $this->multiple = $multiple;
    }
   
    public function getSize() 
    {
        if (!is_null($this->size)) {
            return $this->size;
        }
        return $this->getValueOfBinding('size', 1);
    }
    
    public function setSize($size) 
    {
        $this->size = $size;
    }
    
    public function hasRenderer() 
    {
    	return true;
    }	
   
}